import request from '@/utils/request'
//引入SPU返回的ts类型
import {
  HasSpuResponse,
  AllTradeMark,
  SaleAttrResponseData,
  HasSaleAttrResponseData,
  SpuHasImageResponseData,
  SpuData,
  SkuData,
  HasSkuResponseData,
} from './type'

enum SPU_API_URL {
  /**
   * 获取已有SPU数据的接口地址
   */
  HASSPU_URL = '/admin/product',

  /**
   * 获取全部品牌的数据接口地址
   */
  GET_ALL_TRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',

  /**
   * 获取某SPU下的全部图片数据的接口地址
   */
  GET_SPU_IMAGE_URL = '/admin/product/spuImageList',

  /**
   * 获取某SPU目前销售属性的接口地址
   */
  GET_SPU_ATTR_URL = '/admin/product/spuSaleAttrList',

  /**
   * 获取整个项目全部的销售属性接口地址
   */
  GET_ALL_SALE_ATTR_URL = '/admin/product/baseSaleAttrList',

  /**
   * 增加新的SPU接口地址
   */
  ADD_SPU_URL = '/admin/product/saveSpuInfo',

  /**
   * 修改已有的SPU接口地址
   */
  UPDATE_SPU_URL = '/admin/product/updateSpuInfo',

  /**
   * 添加SKU的接口地址
   */
  ADD_SKU_URL = '/admin/product/updateSpuInfo',

  /**
   * 获取已有SKU数据的接口地址
   */
  GET_SKU_URL = '/admin/product/findBySpuId',

  /**
   * 删除SPU的接口地址
   */
  DELETE_SPU_URL = '/admin/product/deleteSpu',
}

/**
 * 获取三级分类下已有的SPU数据的接口方法
 * @param page
 * @param limit
 * @param category3Id
 * @returns Promise<HasSpuResponse>
 */
export const reqHasSpu = (
  page: number,
  limit: number,
  category3Id: string | number,
): Promise<HasSpuResponse> =>
  request.get<any, HasSpuResponse>(
    SPU_API_URL.HASSPU_URL + `/${page}/${limit}?category3Id=${category3Id}`,
  )

/**
 * 获取全部的SPU品牌的数据的接口方法
 * @returns Promise<AllTradeMark>
 */
export const reqAllTrademark = (): Promise<AllTradeMark> =>
  request.get<any, AllTradeMark>(SPU_API_URL.GET_ALL_TRADEMARK_URL)

/**
 * 获取当前SPU图片数据的接口方法
 * @param spuId
 * @returns Promise<SpuHasImageResponseData>
 */
export const reqImageList = (spuId: number): Promise<SpuHasImageResponseData> =>
  request.get<any, SpuHasImageResponseData>(SPU_API_URL.GET_SPU_IMAGE_URL + `/${spuId}`)

/**
 * 获取当前SPU销售属性的接口方法
 * @param spuId
 * @returns Promise<SaleAttrResponseData>
 */
export const reqAttrList = (spuId: number): Promise<SaleAttrResponseData> =>
  request.get<any, SaleAttrResponseData>(SPU_API_URL.GET_SPU_ATTR_URL + `/${spuId}`)

/**
 * 获取全部的销售属性
 * @returns Promise<HasSaleAttrResponseData>
 */
export const reqAllSaleAttr = (): Promise<HasSaleAttrResponseData> =>
  request.get<any, HasSaleAttrResponseData>(SPU_API_URL.GET_ALL_SALE_ATTR_URL)

/**
 * 添加或新增SPU
 * @param data
 * @returns Promise<any>
 */
export const reqAddOrUpdateSpu = (data: SpuData): Promise<any> => {
  //SPU对象拥有id则是更新
  if (data.id) {
    return request.post<any, any>(SPU_API_URL.UPDATE_SPU_URL, data)
  } else {
    return request.post<any, any>(SPU_API_URL.ADD_SPU_URL, data)
  }
}

/**
 * 添加SKU
 * @param data
 * @returns Promise<any>
 */
export const reqAddSku = (data: SkuData): Promise<any> =>
  request.post<any, any>(SPU_API_URL.ADD_SKU_URL, data)

/**
 * 获取已有SKU数据
 * @param spuId
 * @returns Promise<HasSkuResponseData>
 */
export const reqHasSku = (spuId: number): Promise<HasSkuResponseData> =>
  request.get<any, HasSkuResponseData>(SPU_API_URL.GET_SKU_URL + `/${spuId}`)

/**
 * 删除SPU
 * @param spuId
 * @returns Promise<any>
 */
export const reqDeleteSpu = (spuId: number): Promise<any> =>
  request.delete<any, any>(SPU_API_URL.DELETE_SPU_URL + `/${spuId}`)
